package com.yycx.module.bus.provider.handler.haiying.order;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.interceptor.GetInterceptor;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.bus.client.entity.BusCustom;
import com.yycx.module.bus.client.entity.BusDept;
import com.yycx.module.bus.client.entity.BusSaleman;
import com.yycx.module.bus.client.entity.hy.*;
import com.yycx.module.bus.provider.service.BusCustomService;
import com.yycx.module.bus.provider.service.BusPaymentService;
import com.yycx.module.bus.provider.service.BusSalemanService;
import com.yycx.module.bus.provider.service.hy.*;
import com.yycx.module.system.client.entity.SysBank;
import com.yycx.module.system.provider.service.SysBankService;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 海鹰订单详情信息
 *
 * @author zyf
 */
@Component
public class HyOrderBackGetHandler implements GetInterceptor {


    @Autowired
    private BusHyorderReturnService orderReturnService;


    @Resource
    private HyOrderDetailService orderDetailService;

    @Resource
    private HyOrderService hyOrderService;

    @Override
    public ResultBody validate(CriteriaQuery cq, EntityMap params) {
        return ResultBody.ok();
    }

    @Override
    public void prepare(CriteriaQuery cq, EntityMap entityMap) {

    }

    @Override
    public void complete(CriteriaQuery cq, EntityMap map) {
        String orderId = map.get("orderId");
        List<EntityMap> returnList = orderReturnService.selectMapByOrderId(orderId);
        if (FlymeUtils.isEmpty(returnList)) {
            returnList = orderDetailService.selectMapListByOrderId(orderId);
        }
        for (EntityMap entityMap : returnList) {
            entityMap.put("backNum", 0);
            String orderDetailsId = entityMap.get("orderDetailsId");
            Long orderDetailsReturnId = entityMap.getLong("orderDetailsReturnId");
            EntityMap countMap = orderReturnService.countBackNum(orderDetailsId, orderDetailsReturnId);
            BigDecimal saleNum = entityMap.getBigDecimal("saleNum");
            BigDecimal backNumNo = saleNum;
            BigDecimal backNumYes = new BigDecimal("0");
            if (FlymeUtils.isNotEmpty(countMap)) {
                BigDecimal backNum = countMap.getBigDecimal("saleNum");
                backNumNo = saleNum.add(backNum);
                backNumYes = backNum;
            }
            entityMap.put("backNumYes", backNumYes);
            entityMap.put("backNumNo", backNumNo);
        }
        map.put("returnList", returnList);
    }
}
